package com.lemon.smartsafetynew.ui.activity.patrol;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskItemVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/8/3.
 */

public class SafePatrolDetailActivity extends SwipeBackActivity {

    @Bind(R.id.right_tv)
    TextView rightTv;
    @Bind(R.id.patrol_item_name)
    TextView patrolItemName;
    @Bind(R.id.patrol_location)
    TextView patrolLocation;
    @Bind(R.id.patrol_point)
    TextView patrolPoint;
    @Bind(R.id.maintenance_check)
    TextView maintenanceCheck;
    @Bind(R.id.check_cycle)
    TextView checkCycle;
    @Bind(R.id.building_status)
    TextView buildingStatus;
    @Bind(R.id.patrol_task_describe)
    TextView patrolTaskDescribe;
    @Bind(R.id.patrol_people)
    TextView patrolPeople;
    @Bind(R.id.patrol_date)
    TextView patrolDate;
    @Bind(R.id.safe_patrol_time)
    TextView safePatrolTime;
    @Bind(R.id.consuming_time)
    TextView consumingTime;
    @Bind(R.id.last_check_result_label)
    TextView lastCheckResultLabel;
    @Bind(R.id.last_check_result_icon)
    ImageView lastCheckResultIcon;
    @Bind(R.id.last_status)
    TextView lastStatus;

    private static SafePatrolTaskItemVo safePatrolTaskItemVo;

    @Override
    protected void initTitle() {
        this.setBarTitle("巡查详情");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_patrol_detail;
    }

    @Override
    protected void getBundleArgs() {

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            safePatrolTaskItemVo = (SafePatrolTaskItemVo) bundle.get(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_TASK);
        } else {
            callFinish();
        }
    }

    @Override
    protected void initViews() {
        super.initViews();

        patrolItemName.setText(CommonUtil.avoidNullString(safePatrolTaskItemVo.getObject_title()));
        patrolLocation.setText(CommonUtil.avoidNullString(safePatrolTaskItemVo.getBuilding_name())
                + " - " + safePatrolTaskItemVo.getFloor_number() + "楼 "
                + CommonUtil.avoidNullString(safePatrolTaskItemVo.getPosition_name()));

        if (StringUtils.getBoolean(safePatrolTaskItemVo.getIs_patrol())) {
            patrolPoint.setText("是");
        } else {
            patrolPoint.setText("否");
        }

        if (StringUtils.getBoolean(safePatrolTaskItemVo.getIs_keep_check())) {
            maintenanceCheck.setText("是");
        } else {
            maintenanceCheck.setText("否");
        }

        patrolPeople.setText(CommonUtil.avoidNullString(safePatrolTaskItemVo.getLast_check_user_name()));
        patrolDate.setText(CommonUtil.secondToTimeStr(safePatrolTaskItemVo.getLast_check_date(), DateTypeEnum.DAY));
        safePatrolTime.setText(CommonUtil.timeToStr2(safePatrolTaskItemVo.getLast_check_date()));

        if (safePatrolTaskItemVo.getLast_status() == null) {
            // 没有上次记录 last_status 为null时
            lastCheckResultLabel.setTextColor(Color.parseColor("#448AD0"));
            lastCheckResultIcon.setVisibility(View.GONE);
            lastStatus.setTextColor(Color.parseColor("#605C5B"));
            lastStatus.setText("无");

        } else {

            if (1 == safePatrolTaskItemVo.getLast_status()) { //TODO  现在的状态是1是通过
                lastCheckResultLabel.setTextColor(Color.parseColor("#60CC86"));
                lastCheckResultIcon.setImageResource(R.mipmap.icon_check_pass);
                lastStatus.setTextColor(Color.parseColor("#60CC86"));
                lastStatus.setText("通过");
            } else {
                lastCheckResultLabel.setTextColor(Color.parseColor("#EA654F"));
                lastCheckResultIcon.setImageResource(R.mipmap.icon_check_not_pass);
                lastStatus.setTextColor(Color.parseColor("#EA654F"));
                lastStatus.setText("不通过");
            }
        }

    }

    @OnClick(R.id.right_tv)
    public void onViewClicked() {
        Bundle bundle = new Bundle();
        bundle.putString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID, safePatrolTaskItemVo.getItem_id());
        UIHelper.showActivity(this, bundle, SafePatrolHistoryNewActivity.class);
    }

}
